package com.example.javamediaserver.service;

import com.example.javamediaserver.model.StreamSession;
import com.example.javamediaserver.repository.StreamSessionRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Slf4j
public class StreamSessionService {

    private final StreamSessionRepository repository;
    
    public StreamSession saveSession(StreamSession session) {
        return repository.save(session);
    }
    
    public Optional<StreamSession> findSession(String sessionId) {
        return repository.findById(sessionId);
    }
    
    public List<StreamSession> findActiveSessions() {
        return repository.findByEndTimeIsNull();
    }
    
    public List<StreamSession> findRecordingSessions() {
        return repository.findByIsRecordingTrue();
    }
    
    public void endSession(String sessionId) {
        repository.findById(sessionId).ifPresent(session -> {
            session.setEndTime(LocalDateTime.now());
            repository.save(session);
            log.info("结束会话: {}", sessionId);
        });
    }
    
    public void startRecording(String sessionId, String recordingPath) {
        repository.findById(sessionId).ifPresent(session -> {
            session.setRecording(true);
            session.setRecordingPath(recordingPath);
            repository.save(session);
            log.info("开始录制会话: {}", sessionId);
        });
    }
    
    public void stopRecording(String sessionId) {
        repository.findById(sessionId).ifPresent(session -> {
            session.setRecording(false);
            repository.save(session);
            log.info("停止录制会话: {}", sessionId);
        });
    }
} 